using System;
using System.Collections.Generic;
using System.Text;
using Deepcode.Flux.Core.Systems.CMS;

namespace Deepcode.Flux.Core.UI.Controls
{
	public class NodePath : Base.BaseTextBoxControl
	{
		#region Properties
		/// <summary>
		/// Holds the node id of the parent node
		/// </summary>
		public int NodeIDParent
		{
			get
			{
				object o = ViewState["nidparent"];
				if( o == null ) return -1;
				return (int) o;
			}
			set
			{
				ViewState["nidparent"] = value;
			}
		}
		/// <summary>
		/// Holds the node id of the node we're editing the name/path for
		/// </summary>
		public int NodeIDPathed
		{
			get
			{
				object o = ViewState["nidpathed"];
				if( o == null ) return -1;
				return (int) o;
			}
			set
			{
				ViewState["nidpathed"] = value;
			}
		}
		/// <summary>
		/// Works out and return the parent path string to appear in front of the textbox
		/// </summary>
		public string ParentPath
		{
			get
			{
				ContentManager m = new ContentManager();
				if( NodeIDParent == -1 && NodeIDPathed == -1 ) return "/";

				if (NodeIDParent == -1)
				{
					ContentObject n = m.AdxGetByID(NodeIDPathed);
					if (n == null) return "/";
					return n.NodePath;
				}
				else
				{
					ContentObject n = m.AdxGetByID(NodeIDParent);
					if (n == null) return "/";
					return n.NodePath + n.NodeName + "/";
				}
			}
		}
		#endregion
		
		protected override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			writer.Write("<table cellspacing=0 cellpadding=0 border=0><tr>");
			writer.Write("<td>");
			writer.Write("<span class=\"fluxnodepath\">{0}</span>", ParentPath);
			writer.Write("</td><td>");
			base.Render(writer);
			writer.Write("</td><td><span class=\"fluxnodepath\">.aspx</span></tr></table>");
		}

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			this.LoadClientStyleSheet("NodePath/npstyle.css", "Deepcode.Flux.Core.UI.Controls.NodePath");
		}
	}
}
